const { defineConfig } = require("@vue/cli-service");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const path = require("path");

// 复制文件到指定目录
const copyFiles = [
  {
    from: path.resolve("src/plugins/manifest.json"),
    to: `${path.resolve("aksu-chrome-package")}/manifest.json`,
  },
  {
    from: path.resolve("src/assets"),
    to: path.resolve("aksu-chrome-package/assets"),
  },
  {
    from: path.resolve("src/plugins/inject.js"),
    to: path.resolve("aksu-chrome-package/js"),
  },
];

// 复制插件
const plugins = [
  new CopyWebpackPlugin({
    patterns: copyFiles,
  }),
];

// 页面文件
const pages = {};
// 配置 popup.html 页面
const chromeName = ["popup","options"];

chromeName.forEach((name) => {
  pages[name] = {
    entry: `src/${name}/main.js`,
    template: `src/${name}/index.html`,
    filename: `${name}.html`,
  };
});

module.exports = defineConfig({

  parallel: false, //（问题）vue打包时报错Cannot read property ‘__vueMarkdownOptions__‘ of undefined
  transpileDependencies: true,
  pages,
  productionSourceMap: false,
  outputDir:"aksu-chrome-package",
  // 配置 content.js background.js
  configureWebpack: {
    entry: {
      background: "./src/background/main.js",
    },
    output: {
      // path:path.resolve(__dirname, "jdd-chrome-plugin"),
      filename: "js/[name].js",
    },
    plugins,
    resolve: {
      alias: {
          'utils': path.resolve('src/utils'),
          'src': path.resolve('src')
      }
  }
  },
  // 配置 content.css
  css: {
    extract: {
      filename: "css/[name].css",
    },
  },
  // 增加chainWebpack配置
  chainWebpack: (config) => {
    if (process.env.NODE_ENV === "production") {
      config.output.filename("js/[name].js").end();
      config.output.chunkFilename("js/[name].js").end();
    }
    config.module
      .rule("md")
      .test(/\.md/)
      .use("vue-loader")
      .loader("vue-loader")
      .end()
      .use("vue-markdown-loader")
      .loader("vue-markdown-loader/lib/markdown-compiler")
      .options({
        raw: true,
      });
  },
});
